

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from codecs import open
from os import listdir, getcwd
from sys import stdin

ENCODING = stdin.encoding

def loadFile(filename):
	""" Loads a configuration file
	"""
	dic = {}
	#with open(file, 'rU') as fsocket:
	try:
		fsocket = open(filename, 'rU', 'utf-8')
		lista = fsocket.readlines()
	except IOError:
		print 'Error al abrir el archivo %s; quizás no exista o esté siendo usado por otro proceso' % filename
		raise SystemExit
	else:
		for line in lista:
			if '=' in line and not line.startswith("#"):
				line = line.strip() # To remove the \n
				(key, value) =  line.split('=')
				if value in ['False', 'false']:
					dic[key] = False
				else:
					dic[key] = value
	finally:
		fsocket.close()
		return dic

def ls(arg=False):
	if arg:
		return listdir(arg)
	else:
		return listdir(getcwd())

def list_diff(l, new_l):
	last_old_reg = l[0]
	if last_old_reg in new_l:
		i = new_l.index(last_old_reg)
		return new_l[:i]
	else:
		return new_l

esc_seq = "\x1b["

vcolors={
	'bold':'1',
	'black':"30",
	'lred':"31",
	'green':"32",
	'yellow':"33",
	'blue':"34",
	'magenta':"35",
	'cyan':"36",
	'white':"37",
	'norm':'00',
	'red':'38'
}
colors = {}

for k, v in vcolors.items():
	colors[k] = (k, False)

colors.update({
	'grey':('black', True),
	'orange':('lred', True),
	'yellow':('yellow', True),
	'brown':('yellow', False),
	'skyish':('cyan', True), # Sí, 'skyish' significa celeste aquí xD
	'purple':('blue', True),
	'magenta':('magenta', True),
	'lgreen':('green', True),
	'pink':('magenta', False)
	})
bgcolors = {
	}

def getColorCode(colorcode, light=''):
	if light:
		light = '01;'
	else:
		light = ''
	s = '%s\033[%sm' % (esc_seq, (light + vcolors[colorcode]))
	#print repr(s)
	return s

def getColor(colorname):
	color, light = colors[colorname]
	return getColorCode(color, light)

def formatString(s, color):
	return "%s%s%s" % (getColor(color), s, getColorCode('norm'))

